home *** CD-ROM | disk | FTP | other *** search
-
- Module MainModule
-
- Sub Main()
- ' Run one of the Textxxxx procedures below by uncommenting only one statement
-
- 'TestInterface()
- 'TestInterfaceOnStructure()
- 'TestIComparable()
- 'TestIComparer()
- 'TestIComparer2()
- 'TestICloneable()
- 'TestICloneable2()
- 'TestICloneable3()
- 'TestICloneable4()
- 'TestIEnumerable()
- 'TestGetEnumerator()
-
- ' You need these statements when running inside Visual Studio, so that
- ' the Console window doesn't disappear
- Console.WriteLine("")
- Console.WriteLine(">>> press Enter to terminate the program <<<")
- Console.ReadLine()
- End Sub
-
- ' the following procedure accesses an interface of a class
-
- Sub TestInterface()
- ' An instance of the class
- Dim addin As New MyAddin()
- ' Cast to an interface variable.
- Dim iplug As IPluggableAddin = addin
- ' Now you can access all the methods and properties in the interface.
- iplug.State = True
-
- ' Cast to the interface type and invoke a method in one operation.
- DirectCast(addin, IPluggableAddin).OnConnection("MyHost")
-
- ' Create a hidden, temporary interface variable.
- With DirectCast(addin, IPluggableAddin)
- .OnConnection("MyHost")
- .State = True
- End With
-
- Dim addin2 As New AnotherAddin()
- ' Prove that the AnotherAddin inherited class exposes the IPluggableAddin interface.
- DirectCast(addin2, IPluggableAddin).State = True
-
- ' The Dispose method in the derived class invokes the MyAddin.Dispose method
- addin2.Dispose()
- End Sub
-
- ' Test interfaces on structures
-
- Sub TestInterfaceOnStructure()
- ' this code proves that value types are boxed when an interface method is called
- Dim struc As MyStruct
-
- ' Get the object reference from the structure
- Dim o1 As Object = CType(struc, IGetObjectRef).GetObjectRef
- ' Do the same once again
- Dim o2 As Object = CType(struc, IGetObjectRef).GetObjectRef
-
- ' prove that the object has been boxed twice to different areas in the heap
- Console.WriteLine(o1 Is o2) ' => False
- End Sub
-
- ' This procedure demonstrates the IComparable interface
-
- Sub TestIComparable()
- Dim Persons() As Person = {New Person("John", "Smith"), _
- New Person("Robert", "Doe"), New Person("Joe", "Doe")}
- Array.Sort(Persons)
-
- ' Print all the elements in sorted order.
- Dim p As Person
- For Each p In Persons
- Console.WriteLine(p.ReverseName)
- Next
- End Sub
-
- ' this procedure demonstrates how you can use the IComparer interface
-
- Sub TestIComparer()
- Dim Persons() As Person2 = {New Person2("John", "Smith"), _
- New Person2("Robert", "Doe"), New Person2("Joe", "Doe")}
-
- ' Sort the array on name.
- Array.Sort(Persons, New Person2.ComparerByName())
- ' another way to reach the same result using a shared method
- Array.Sort(Persons, Person2.CompareByName)
-
- ' Print all the elements in sorted order.
- Console.WriteLine("Elements sorted on name:")
- Dim p As Person2
- For Each p In Persons
- Console.WriteLine(p.CompleteName)
- Next
-
- ' Sort the array on reversed name.
- Array.Sort(Persons, New Person2.ComparerByReverseName())
- ' another way to reach the same result using a shared method
- Array.Sort(Persons, Person2.CompareByReverseName)
-
- Console.WriteLine("Elements sorted on reverse name:")
- For Each p In Persons
- Console.WriteLine(p.ReverseName)
- Next
- End Sub
-
- ' this procedure tests sorts in case sensitive way
-
- Sub TestIComparer2()
- Dim arr() As String = {"aaa", "AAA", "Abc", "ABC", "abc", "aBC", "aaα", "αbc"}
- Dim s As String
-
- Array.Sort(arr)
- Console.WriteLine("--- Standard sort")
- For Each s In arr
- Console.WriteLine(s)
- Next
-
- Array.Sort(arr, CaseInsensitiveComparer.Default)
- Console.WriteLine("--- Sort using CaseInsensitiveComparer.Default")
- For Each s In arr
- Console.WriteLine(s)
- Next
-
- Array.Sort(arr, New CaseInsensitiveComparerVB6())
- Console.WriteLine("--- Sort using CaseInsensitiveComparerVB6")
- For Each s In arr
- Console.WriteLine(s)
- Next
- End Sub
-
- ' this procedure demonstrates the ICloneable interface
-
- Sub TestICloneable()
- ' Define an employee and his boss.
- Dim joe As New Employee("Joe", "Doe")
- Dim robert As New Employee("Robert", "Smith")
- joe.Boss = robert
-
- ' Clone it û The Clone method returns an object,
- ' thus you need DirectCast if Option Strict is On.
- Dim joe2 As Employee = DirectCast(joe.Clone, Employee)
-
- ' prove that all properties were copied
- Console.WriteLine(joe2.FirstName & " " & joe2.LastName _
- & ", whose boss is " & joe2.Boss.FirstName & " " & joe2.Boss.LastName)
- ' => Cloned object: Joe Doe, whose boss is Robert Smith
- End Sub
-
- ' this procedure proves that Clone does shallow copy
-
- Sub TestICloneable2()
- ' Define an employee and its boss
- Dim joe As New Employee("Joe", "Doe")
- Dim robert As New Employee("Robert", "Smith")
- joe.Boss = robert
- ' Clone it
- Dim joe2 As Employee = CType(joe.Clone, Employee)
- ' Prove that the Employee object was cloned, but his boss wasn't.
- Console.WriteLine(joe Is joe2) ' => False
- Console.WriteLine(joe.Boss Is joe2.Boss) ' => True
- End Sub
-
- ' this procedure shows a deep copy clone method
-
- Sub TestICloneable3()
- ' Define an employee and its boss
- Dim joe As New Employee2("Joe", "Doe")
- Dim robert As New Employee2("Robert", "Smith")
- joe.Boss = robert
- ' Clone it
- Dim joe2 As Employee2 = CType(joe.Clone, Employee2)
- ' Prove that we clone the entire object graph.
- Console.WriteLine(joe Is joe2) ' => False
- Console.WriteLine(joe.Boss Is joe2.Boss) ' => False
- End Sub
-
- ' this procedure tests a strong-typed Clone method
-
- Sub TestICloneable4()
- ' Define an employee and its boss
- Dim joe As New Employee3("Joe", "Doe")
- Dim robert As New Employee3("Robert", "Smith")
- joe.Boss = robert
- ' Clone it - note that no unboxing occurs here.
- Dim joe2 As Employee3 = joe.Clone
- ' Prove that we clone the entire object graph.
- Console.WriteLine(joe Is joe2) ' => False
- Console.WriteLine(joe.Boss Is joe2.Boss) ' => False
- End Sub
-
- ' this procedure demonstrates the IEnumerable and IEnumerator interfaces.
-
- Sub TestIEnumerable()
- Dim msg As String = "Please, split this into individual words!"
- Dim o As Object
- For Each o In New WordParser(msg)
- Console.WriteLine(o)
- Next
- End Sub
-
- ' this procedure tests direct interaction with the GetEnumerator function
-
- Sub TestGetEnumerator()
- Dim f As System.IO.FileInfo
-
- ' enumerate all files in that directory tree
- For Each f In New FileTree("C:\Docs")
- Console.WriteLine(f.FullName)
- Next
- End Sub
-
- End Module
-
-